package com.haierCamera.customapplication.ui;


import android.content.Intent;
import android.content.BroadcastReceiver;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.haierCamera.customapplication.BaseFragment;
import com.haierCamera.customapplication.Constants;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.api.vo.GetDeviceEntity;
import com.haierCamera.customapplication.databinding.HzklFragmentMsgBinding;
import com.haierCamera.customapplication.ui.user.send.HZKLSystemMsgActivity;
import com.haierCamera.customapplication.ui.user.send.HZKLUserMsgActivity;
import com.haierCamera.customapplication.ui.business.Business;
import com.haierCamera.customapplication.ui.business.entity.AlarmMessageInfo;
import com.haierCamera.customapplication.ui.main.AlarmMessageListAdapter;
import com.haierCamera.customapplication.utils.DateUtil;
import com.haierCamera.customapplication.utils.SharedPreferencesUtils;
import com.haierCamera.customapplication.utils.android.HttpErrorProcess;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import dagger.Lazy;

public class MsgFragment extends BaseFragment {

    //    @Inject
//    ViewModelProvider.Factory factory;
    @Inject
    Lazy<HttpErrorProcess> httpErrorProcessLazy;

    HzklFragmentMsgBinding binding;
    BroadcastReceiver mReceiver;

    List<AlarmMessageInfo> list_a = new ArrayList<>();
    AlarmMessageListAdapter adapter;

    int s = 0;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.hzkl_fragment_msg, container, false);

        initData();
        binding.rcv.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new AlarmMessageListAdapter(getActivity());
        binding.rcv.setAdapter(adapter);

        binding.swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                list_a.clear();
                getDeviceMsg(0,0);
            }
        });

        if (Business.getInstance().getmDeviceList() != null) {
            s = Business.getInstance().getmDeviceList().size();
        } else {
            s = 0;
        }
        return binding.getRoot();
    }

    private void initData() {
        binding.layout2.setOnClickListener(view -> startActivity(new Intent(getActivity(), HZKLSystemMsgActivity.class)));
        binding.layout.setOnClickListener(view -> startActivity(new Intent(getActivity(), HZKLUserMsgActivity.class)));
    }

    private void clearList() {
        if (list_a != null && list_a.size() != 0) {
            list_a.clear();
            adapter.upDate(list_a);
        }
    }

    private void getDeviceMsg(int i,int channel) {

        if (Business.getInstance().getmDeviceList() != null && Business.getInstance().getmDeviceList().size() > 0) {
//            ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(Business.getInstance().getmDeviceList().size(), 5,
//                    1, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>(128));
//
            GetDeviceEntity entity = Business.getInstance().getmDeviceList().get(i);
            Business.getInstance().queryAlarmMessageList2(entity.serialNumber
                    , entity.securityCode
                    , DateUtil.getSevenDayBefore().substring(0, 10) + " 00:00:00"
                    , DateUtil.getNowTime().substring(0, 10) + " 23:59:59"
                    , 1
                    , "-1"
                    , channel +""
                    , new Handler() {
                        @Override
                        public void handleMessage(@NonNull Message msg) {
                            super.handleMessage(msg);
                            Log.i("TAG", "handleMessage-" + msg.what);
                            if (msg.what == 0) {
                                List<AlarmMessageInfo> list = (List<AlarmMessageInfo>) msg.obj;

                                if (list.size() > 0) {
//                                                Log.i("TAG","AlarmMessageInfoList-getDeviceId-" + list.get(0).getDeviceId());

                                    list.get(0).setChnlUuid(channel + "");
                                    list.get(0).setName(entity.name + "-" + (channel + 1));

                                    list_a.addAll(list);
                                    adapter.upDate(list_a);

                                    Business.getInstance().getmAlarmMessageInfoList().clear();
                                }

                                if ((i + 1) < s && (channel+1) <entity.channelNum) {
                                    getDeviceMsg(i, channel + 1);
                                } else if ((i + 1) < s && (channel+1) ==entity.channelNum) {
                                    getDeviceMsg(i + 1, 0);
                                }else if ((i + 1) == s) {
                                    binding.swipe.setRefreshing(false);
                                }
                                if (list_a.size() > 0) {
                                    binding.rcv.setVisibility(View.VISIBLE);
                                    binding.ivEmpty.setVisibility(View.GONE);
                                } else {
                                    binding.rcv.setVisibility(View.GONE);
                                    binding.ivEmpty.setVisibility(View.VISIBLE);
                                }

                            } else {
                                binding.swipe.setRefreshing(false);
                            }
                        }
                    });

        }
        binding.swipe.setRefreshing(false);
    }


    @Override
    public void onResume() {
        super.onResume();
        Log.i("TAG", "MsgFragment-onResume");

        String token = SharedPreferencesUtils.getString(Constants.SP_USER_TOKEN, "");
        if (TextUtils.isEmpty(token)) {

        } else {
            clearList();
            getDeviceMsg(0,0);
        }

    }

    @Override
    public void onPause() {
        super.onPause();
        if (binding.swipe.isRefreshing()) {
            binding.swipe.setRefreshing(false);
        }
    }

}
